package br.com.portalecbahia.connectionservices;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import br.com.portalecbahia.listner.OnCompleateAudio;
import br.com.portalecbahia.util.PortalECBahiaUtil;

public class SynchronizeAudio extends Service implements OnCompletionListener { 

	MediaPlayer player;
	public static OnCompleateAudio listeners;

	@Override 
	public IBinder onBind(Intent intent) { 
		return null; 
	} 

	@Override 
	public void onCreate() { 
		Log.d(PortalECBahiaUtil.TAG, "SynchronizeAudio onCreate"); 
	
	} 
	

	@Override 
	public void onDestroy() { 
		Log.d(PortalECBahiaUtil.TAG, "SynchronizeAudio onDestroy"); 

		player.stop();
		listeners.onCompleateAudio();

	} 
	
	@Override 
	public void onStart(Intent intent, int startid) { 
		Log.d(PortalECBahiaUtil.TAG, "SynchronizeAudio onStart"); 
		
		player = MediaPlayer.create(this, Uri.parse(PortalECBahiaUtil.AUDIO_PLAYING));
		if(player!=null){
		player.setOnCompletionListener(this);
		player.start();
		player.setLooping(false); // Set looping 
		}
		player.start(); 
	} 
	
	public void onCompletion(MediaPlayer mp) {
		  // TODO Auto-generated method stub
		 Log.d(PortalECBahiaUtil.TAG,"Media is completed");
		 
		 listeners.onCompleateAudio();
		
	}
} 